#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // 创建QSerialPort对象
    serial = new QSerialPort(this);
    // 检测可用串口
    ReflashSerialPort();
    // 串口读操作
    connect(serial, &QSerialPort::readyRead, this, &MainWindow::SerialRead);
    // 文本设置
    ui->RX->setText(QString("RX:%1").arg(0));
    ui->TX->setText(QString("TX:%1").arg(0));
    ui->RN_checkBox->setText("\\r\\n");
    ui->RN_checkBox->setChecked(true);
    //隐藏多次发送
    HideMultiSend(0);

    // 检测可用IP
    listLocalIPs();

    // 信号
    connect(ui->Find_lineEdit, &QLineEdit::textChanged, this, &MainWindow::SerialFindString);
    connect(ui->NetWorkFind_lineEdit, &QLineEdit::textChanged, this, &MainWindow::NetWorkFindString);
}

void MainWindow::msgbox(QString text)
{
    QDialog *messageDialog = new QDialog();
    QVBoxLayout *layout = new QVBoxLayout(messageDialog);
    QLabel *messageLabel = new QLabel();
    messageLabel->setText(text);
    QFont font = messageLabel->font();
    font.setPointSize(12);
    messageLabel->setFont(font);
    messageLabel->setAlignment(Qt::AlignCenter);
    layout->addWidget(messageLabel);
    messageDialog->setLayout(layout);
    messageDialog->setWindowTitle("警告");
    messageDialog->resize(200, 60);
    messageDialog->show();

    QTimer::singleShot(1000, messageDialog, &QDialog::close); // 500毫秒后执行关
}

//隐藏多次发送
void MainWindow::HideMultiSend(bool arg)
{
    for (int i = 0; i < ui->Cmd_Layout->count(); ++i) {
        QWidget *widget = ui->Cmd_Layout->itemAt(i)->widget();
        if (widget) {
            if (arg)
                widget->show();
            else
                widget->hide();
        }
    }
}

// 修改接收区指定字符串颜色
void MainWindow::ChangeReceivePlainTextEditSubStringColor(QPlainTextEdit *Receive_TextEdit, QLineEdit *Find_lineEdit)
{
    // 创建一个文本格式对象，设置字体颜色为蓝色
    QTextCharFormat blueFormat;
    blueFormat.setForeground(QColor(Qt::red));

    // 获取当前文本编辑器中的文本
    QString text = Receive_TextEdit->toPlainText();
    QString SrcText = Find_lineEdit->text();

    // 创建一个文本光标
    QTextCursor cursor = Receive_TextEdit->textCursor();

    // 重置光标位置到文本开始
    cursor.movePosition(QTextCursor::Start);

    int index = -1;
    // 在文本中查找字符串 SrcText 并应用格式
    while ((index = text.indexOf(SrcText, index + 1)) != -1) {
        cursor.setPosition(index);
        cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, SrcText.size());
        cursor.mergeCharFormat(blueFormat);
    }

    // 确保更新文本编辑器显示
    Receive_TextEdit->mergeCurrentCharFormat(blueFormat);
}

// 恢复字符颜色
void MainWindow::ChangeReceivePlainTextEditAllStringColor(QPlainTextEdit *Receive_TextEdit)
{
    QTextCharFormat charFormat;
    charFormat.setForeground(QColor(Qt::black));

    // 获取QPlainTextEdit的文本光标
    QTextCursor cursor = Receive_TextEdit->textCursor();

    // 移动光标到文档的开始位置
    cursor.movePosition(QTextCursor::Start);

    // 选择整个文档
    cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);

    // 应用黑色格式到整个文档
    cursor.mergeCharFormat(charFormat);
}


MainWindow::~MainWindow()
{
    delete ui;
}

/******************************************************
 *                      信号
*****************************************************/
#include <QPlainTextEdit>
#include <QTextCursor>
#include <QTextCharFormat>
#include <QColor>
void MainWindow::SerialFindString(const QString &text)
{
    if (ui->Find_checkBox->isChecked() && text.size() > 0)
    {
        ChangeReceivePlainTextEditAllStringColor(ui->Receive_TextEdit);
        ChangeReceivePlainTextEditSubStringColor(ui->Receive_TextEdit, ui->Find_lineEdit);
    }
    else
        ChangeReceivePlainTextEditAllStringColor(ui->Receive_TextEdit);
}
void MainWindow::NetWorkFindString(const QString &text)
{
    if (ui->NextWorkFind_checkBox->isChecked() && text.size() > 0)
    {
        ChangeReceivePlainTextEditAllStringColor(ui->network_plainTextEdit);
        ChangeReceivePlainTextEditSubStringColor(ui->network_plainTextEdit, ui->NetWorkFind_lineEdit);
    }
    else
        ChangeReceivePlainTextEditAllStringColor(ui->network_plainTextEdit);
}
